package entity

import (
	"gluenet/modules/manager/server/domain/po"
	"gluenet/modules/manager/server/domain/vo"
	"gluenet/modules/manager/server/infra"
	"strings"
)

const (
	PlatformGluenets   = "gluenets"
	PlatformKubernetes = "kubernetes"

	KindApplication = "application"
	KindStrategy    = "strategy"
	KindAgent       = "agent"
)

type Config struct {
	infra.Entity `xorm:"-"`
	po.Config    `xorm:"extends"`
	Tags         []*po.Tag `xorm:"-"`
}

func (r *Config) Identity() string {
	return r.ID
}

func (r *Config) ValidateFormat() error {
	return nil
}

func (r *Config) MarkedTag(key, value string) error {
	return nil
}

func (r *Config) Update(new *vo.UpdateConfigReq) error {
	r.Name = new.Name
	r.Describe = new.Describe
	return nil
}

func (r *Config) ShowInfo() *vo.ConfigInfoRes {
	inf := &vo.ConfigInfoRes{
		ID:         r.ID,
		Name:       r.Name,
		Kind:       r.Kind,
		Version:    r.Version,
		Platform:   r.Platform,
		Describe:   r.Describe,
		Label:      r.Label,
		DetailInfo: r.DetailRepoValue,
		Created:    r.Created,
		Updated:    r.Updated,
	}
	if inf.Platform == PlatformKubernetes {
		inf.Name = strings.Join(strings.Split(inf.Name, "-")[1:], "-")
	}
	return inf
}

func (r *Config) New(req vo.CreateConfigReq) error {
	r.ID = infra.NewID()
	r.UserID = req.UserID
	r.Name = req.Name
	r.Kind = req.Kind
	r.Platform = req.Platform
	r.Label = req.Label
	r.Describe = req.Describe
	r.Version = req.Version
	r.DetailRepoKey = req.DetailRepoKey
	r.DetailRepoValue = req.DetailRepoValues
	return nil
}
